﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace TripLOA.GUI.secure
{
    public partial class acceptPassenger : TripLOA.GUI.internationalization.LOABasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            map_1.GoogleApiKey = System.Configuration.ConfigurationManager.AppSettings["googlekey"];
            if (!IsPostBack)
            {
                
                int tripID=0;
                int passID=0;
                try
                {
                    tripID = int.Parse(Request.QueryString["trip"]);
                    passID = int.Parse(Request.QueryString["pass"]);
                }
                catch (FormatException)
                {
                    Response.Redirect("CreatedTrips.aspx");
                }

                if (Request.QueryString["repeat"] != null)
                {
                    lblException.Visible = true;
                }

                map_1.TargetControlIDs = btnAccept.ClientID;
                Security.TripLoaUser us = Security.TripLoaUserManager.Instance.getUser(User.Identity.Name);
                Storage.EntityModels.Trip strip = Groupware.TripManager.GetDbTripById(tripID);
                GeoRef.GeoTrip geotrip = new GeoRef.GeoTrip(strip);
                map_1.EditableTrip = geotrip;
                Groupware.ReservationRequest grres = (Groupware.ReservationRequest)Session["reservation"];
                GeoRef.GeoLoc start = grres.Start;
                GeoRef.GeoLoc end = grres.End;
                float startdev = grres.StartTolerance;
                float enddev = grres.EndTolerance;
                map_1.SetPassenger(passID, start, startdev, end, enddev);
                map_1.SetHelp((String)HttpContext.GetGlobalResourceObject("TripLoaResource", "HelpAccept"));
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            GeoRef.GeoTrip gt = map_1.EditableTrip;
            
            int idpassenger = int.Parse(Request.QueryString["pass"]);
            Security.TripLoaUser secuser = Security.TripLoaUserManager.Instance.getUser(User.Identity.Name);
            Groupware.Trip trip = Groupware.TripManager.GetTripById(int.Parse(Request.QueryString["trip"]));
            Groupware.ReservationRequest grequest = (Groupware.ReservationRequest)Session["reservation"];
            try
            {
                trip.ConfirmReservation(secuser, gt, grequest);
                // deletes the notification for this reservation using UserId
                Groupware.Notification n = trip.GetReservationRequestNotification(grequest.UserId);
                if(n != null) n.Delete();
                lblOK.Visible = true;
                btnRedirect.Visible = true;
                UpdatePanelOK.Visible = true;
                UpdatePanelOK.Update();
            }
            catch (Exception)
            {
                Server.Transfer("acceptPassenger.aspx?trip=" + trip.Id + "&pass=" + idpassenger + "&repeat=yes");
            }
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            int id =int.Parse(Request.QueryString["trip"]);
            Response.Redirect("tripDetail.aspx?id=" + id);
        }

        protected void RedirectOtherPage(object sender, EventArgs e)
        {
            Groupware.Trip trip = Groupware.TripManager.GetTripById(int.Parse(Request.QueryString["trip"]));
            Response.Redirect("tripDetail.aspx?id=" + trip.Id);
        }
    }
}
